package com.xilihui.xlh.business.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.BookingDetailsActivity;
import com.xilihui.xlh.business.adapters.HistoryAdapter;
import com.xilihui.xlh.business.entities.HistoryEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/25.
 */

public class AppointmentOrderFragment extends BaseCompatFragment implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {

    public static final String COMPLETE = "complete";
    public static final String CANCEL = "cancel";
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ExceptionManager exceptionManager;
    
    HistoryAdapter historyAdapter;
    
    int page = 1;
    int status = 0;
    ArrayList<HistoryEntity.AppointBean> datas = new ArrayList<>();
    
    
    public AppointmentOrderFragment(){
        
    }
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_appoint_order,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        getExtras();
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_lsjl;
            }
        };
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        historyAdapter = new HistoryAdapter(getActivity());
        
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(historyAdapter);
        getDatas(true,page);
        
    }

    private void getExtras() {
        int type = getArguments().getInt("type");
        if(type==0){
            status = 3;
        }else if(type==1){
            status = 9;
        }
    }
    
    public void getDatas(boolean isLoading, final int page){

        MyAppointmentRequest.history(getActivity(),status,page)
                .compose(DoTransform.<HistoryEntity>applyScheduler(getActivity(),isLoading))
                .compose(this.<HistoryEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<HistoryEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(HistoryEntity entity) {
                        
                        if(page==1){
                            datas.clear();
                            if(entity.getAppoint().size()<1){
                                exceptionManager.showEmpty();
                            }else {
                                exceptionManager.hide();
                            }
                        }
                        
                        datas.addAll(entity.getAppoint());
                        historyAdapter.setDatas(datas);
                                                
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getDatas(false,page);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getDatas(false,page);
    }

    @Override
    public void onClick(View view) {
        
    }
}
